package Long.Lu.Controller;

import Long.Lu.Service.ArchivesService;
import Long.Lu.Service.ContractService;
import Long.Lu.Service.EmployeeService;
import Long.Lu.pojo.Archives;
import Long.Lu.pojo.Contract;
import Long.Lu.pojo.Employee;
import Long.Lu.pojo.EmployeeParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/deployment")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private ArchivesService archivesService;

    //员工信息页面
    @RequestMapping("/employee-list")
    public String EmployeeList(ModelMap modelMap, EmployeeParam employeeParam) {
        Page<Employee> employeePage = this.employeeService.select(employeeParam);
        modelMap.addAttribute("lists", employeePage.getContent());
        modelMap.addAttribute("totalPage", employeePage.getTotalPages());
        modelMap.addAttribute("totalElments", employeePage.getTotalElements());
        if (employeePage.getNumber() <= 0) {
            modelMap.addAttribute("pre", 1);
        } else {
            modelMap.addAttribute("pre", employeePage.getNumber());
        }
        if ((employeePage.getNumber() + 2) >= employeePage.getTotalPages()) {
            modelMap.addAttribute("next", employeePage.getTotalPages());
        } else {
            modelMap.addAttribute("next", employeePage.getTotalPages() + 2);
        }
        modelMap.addAttribute("cur", employeePage.getNumber() + 1);
        modelMap.addAttribute("last", employeePage.getTotalPages());

        modelMap.addAttribute("eName", employeeParam.geteName());
            return "deployment/employee-list";
        }

        //合同页面
        @RequestMapping("/contract-list")
        public String ContractList (ModelMap modelMap) {
        List <Contract> contractList = contractService.selectAll();
        modelMap.addAttribute("list", contractList);
        return "/deployment/contract-list";
        }

        //修改员工状态
        @RequestMapping("/updateStatus")
        @ResponseBody
        public boolean updateStatus (@RequestParam("employId") Integer employId){

            return employeeService.updateStatus(employId);
        }

        //档案管理页面
        @RequestMapping("/archives-list")
        public String ArchivesList (ModelMap modelMap){
            List<Archives> archivesList = archivesService.selectAll();

            modelMap.addAttribute("aList", archivesList);
            return "/deployment/archives-list";
        }

        //员工新增页面
        @RequestMapping("/employee-add")
        private String EmployeeAdd (Model model){
            return "/deployment/employee-add";
        }

        @RequestMapping("/save/{path}")
        @ResponseBody
        public boolean save (Employee employee, @PathVariable("path") String path){
            if (path.equals("employee-add")) {
                return employeeService.add(employee);
            }
            return false;
        }
        //删除员工
        @RequestMapping("/deleteOne")
        @ResponseBody
        public boolean deleteOne (@RequestParam("employId") Integer employId){
            employeeService.deleteOne(employId);
            return true;
        }
        //批量删除
        @RequestMapping("/delAll")
        @ResponseBody
        public boolean delAll (String employIds){
            return employeeService.delAll(employIds);
        }

        //文件导出
        @RequestMapping("/excelWrite")
        public void excelWrite (HttpServletResponse response){
            employeeService.excelWrite(response);
        }
}
